<template>
	<div class="orgManagerWrap" v-if="this.addOrgPage==true">
		<alert ref="alert"></alert>
		<div class="bd_line">
			<span>组织名称:</span>
			<input type="text" placeholder="请输入组织名称" v-model="name">
		</div>
		<div class="bd_line" v-if="this.$parent.nowType=='edit'">
			<span>组织编号:</span>
			<em>{{serialNumber}}</em>
		</div>
		<div class="bd_line">
			<span>所在地区:</span>
			<el-cascader v-model="valueWrap" :options="cityArr" :props="optionProps" placeholder="选择地区" @change="cityArrChangeAdd"></el-cascader>
		</div>
		<div class="bd_line" style="height: 80px;">
			<span>组织描述:</span>
			<textarea placeholder="请输入更新内容..." v-model="describes" style="position: absolute;left: 112px;top: 0;"></textarea>
		</div>
		<div class="footerButton">
			<a @click="checkSave">保存</a>
			<a @click="cancel" class="cancel">取消</a>
		</div>
	</div>
</template>

<script>
	import alert from '../components/Alert.vue';
	export default{
		data(){
			return{
				addOrgPage:false,
				nowType:'childAdd',
				cityArr:[],
				proviceid:'',
				cityid:'',
				regionid:'',
				optionProps: {
					value: 'id',
					label: 'name',
					children: 'list',
					checkStrictly: true
				},
				valueWrap:[],
				
				name:'',
				serialNumber:'',
				describes:'',
				proviceidWrap:'',
				cityidWrap:'',
				regionidWrap:'',
				dataId:''
			}
		},
		methods:{
			cityArrChangeAdd(data){
				this.proviceid = data[0];
				if(data[1]!=undefined){
					this.cityid = data[1];
				}else{
					this.cityid = '';
				};
				if(data[2]!=undefined){
					this.regionid = data[2];
				}else{
					this.regionid = '';
				};
			},
//			add_type(){
//				let s= {
//					rootcompanyid:1,
//					name:this.name,
//					pid:this.pid,
//					proviceid:this.proviceid,
//					cityid:this.cityid,
//					regionid:this.regionid,
//					describes:this.describes
//				};
//				if(this.name==''){
//					this.wrapClose = true;
//					this.stateWrapText = '组织名称为空！';
//				}else{
//					this.$ajax({
//						url: this.$root.jifuUrl + '/addOrganization',
//						method: 'POST',
//						data: this.qs.stringify(s),	
//						success: res => {
//							if(res.data.code == 200) {
//								this.$parent.addOrgPage = false;
//								this.$rootFunction().addResSuccess();
//							}else{
//								this.failText = res.data.message;
//								this.$rootFunction().deleteResFail();
//							}
//						}
//					});
//				}
//			},
//			addOrganization(row){
//				this.selectRow=row;
//				this.addOrgPage = true;
//				this.$parent.nowType = 'childAdd';
//				this.transitionAddId = row.id;
//			},
			add_child_type(){
				console.log(5555,this.$parent.dataId);
				let s= {
					rootcompanyid:1,
					name:this.name,
					pid:this.$parent.dataId,
					proviceid:this.proviceid,
					cityid:this.cityid,
					regionid:this.regionid,
					describes:this.describes
				};
				if(this.name==''){
					this.wrapClose = true;
					this.stateWrapText = '组织名称为空！';
				}else{
					this.$ajax({
						url: this.$root.jifuUrl + '/addOrganization',
						method: 'POST',
						data: this.qs.stringify(s),	
						success: res => {
							if(res.data.code == 200){
								this.addOrgPage = false;
								this.$refs.alert.show({
									alertText: '保存成功',
									alertSubmit: function() {
										alertStatus: 0;
										that.getTableList();
									}
								});
								this.$rootFunction().addResSuccess();
							}else{
								this.failText = res.data.message;
								this.$rootFunction().deleteResFail();
							}
						}
					});
				}
			},
			edit_type(){
				if(this.cityid==''){
					this.cityid=-1;
				};
				if(this.regionid==''){
					this.regionid=-1;
				};
				let s= {
					rootcompanyid:1,
					name:this.name,
					id:this.editId,
					pid:this.editPid,
					proviceid:this.proviceid,
					cityid:this.cityid,
					regionid:this.regionid,
					describes:this.describes
				};
				this.$ajax({
					url: this.$root.jifuUrl + '/updateOrganization',
					method: 'POST',
					data: this.qs.stringify(s),	
					success: res => {
						if(res.data.code == 200) {
							this.addOrgPage = false;
							this.wrapClose = true;
							this.stateWrapText = '保存成功';
							setTimeout(() =>{
								this.$router.go(0);
							},2000)
						}else{
							this.addOrgPage = false;
							this.wrapCloseSecond = true;
							this.stateWrapTextSecond = res.data.message;	
						}
					}
				});
			},
			checkSave(){
				if(this.nowType == 'add') {
					this.add_type();
				}else if(this.nowType == 'childAdd'){
					this.add_child_type();
				}else if(this.nowType == 'edit') {
					this.edit_type();
				} 
//				if(this.$parent.nowType == 'childAdd'){
//					this.add_child_type();
//				}
				
			},
			cancel() {
				this.addOrgPage = false;
				this.name = '';
				this.valueWrap = [];
				this.describes = '';
			},
			getTreeDataCity(data){
				for(var i=0;i<data.length;i++){
					if(data[i].list.length<1){
						data[i].list=undefined;
					}else {
						this.getTreeDataCity(data[i].list);
					}
				}
				return data;
        	}
		},
		components: {
			alert
		},
		mounted(){
			this.$rootFunction().getAllCity();
		}
	}
</script>